var myapp=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  
      when('/', {templateUrl: 'out_ngrepeat_ajs.html', controller: outrptctrl}).
	  //when('/statemnt/', {templateUrl: 'out_ngrepeat_ajs.html', controller: outrptctrl}). 
	 // when('/ledger/', {templateUrl: 'out_ngrepeat_ajs.html', controller: outrptctrl}). 
    otherwise({redirectTo: '/'});
}]);

myapp.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
    
  $scope.outstandingmast = {};
  $scope.loading = true;


  
function outrptctrl($scope, $http,$window) {
//$scope.dis_search = false;	
//$http.get('getviewuser.php').success(function(data,status) 
//	{
//		$scope.name = data;		
//		if($scope.name.length > 1)
//			$scope.dis_search = true;
//	});	
$http.get('outstandingdata_sendemail.php').success(function(data,status) 
  {
    $scope.currentPage = 0;
    $scope.pageSize = 75;
    $scope.Math = window.Math;
    $scope.outstandingmast = data;
    $scope.selectionid = [];
    $scope.selectionname = [];
    $scope.selectionamt = [];
    $scope.url = '';
  //  $window.alert("Total Customer  "+data.length);
  
  var d1 = new Date();	
  var d2 = ('0' + (d1.getMonth()+1)).slice(-2)+"/"+('0' + d1.getDate()).slice(-2) +"/"+d1.getFullYear();
  document.getElementById('datepicker1').value=d2;
   
     $scope.loading = false;
  }).error(function(data, status) {
      $scope.status = 'Error getting Outstanding ! ' + data;
      $scope.loading = false;
    });        
  
    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
	
$scope.sendsms=function()
    {
		if($scope.selectionid.length==0)		
			alert("you have not made any selections");
		else 
		{										
			for (i=0;i<$scope.selectionid.length;i++)
			{		
				var tosms="getsendsms.php?&rcv="+$scope.selectionname[i]+"&amt="+$scope.selectionamt[i];	
				//alert(tosms);
				$http.post(tosms).success(function(data)
				{
					$scope.url = data;	
					$scope.sms($scope.url);					
				});								
			}
			alert("SMS sent");	
			var x = document.getElementById("status").innerHTML;						
			document.getElementById('status').innerHTML=x+"<br/><b>SMS sent </b>";
		}
    };
	$scope.sms=function(url)
    {		
		$.post( url, function() {
		  alert( "success" );
		});
	};
	$scope.ssendemail=function()
    {		
		if($scope.selectionid.length==0)		
			alert("you have not made any selections");
		else 
		{
			var d = new Date();	
			var f1=document.getElementById("datepicker1").value;
			var f3 = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();	
			if(!f1)			
				var f4=f3;			
			else			
			{
				var f2 = new Date(f1);
				var f4 = ((f2.getDate())+"-"+(f2.getMonth()+1)+"-"+f2.getFullYear());
			}	
			for (i=0;i<$scope.selectionid.length;i++)
			{			
				var sql="report_ledger_bydate_sendemail.php?custid="+$scope.selectionid[i]+"&custname="+$scope.selectionname[i]+"&date="+f3+"&startdate="+f4+"&flag=1";								
				$http.post(sql).success(function(data){});
			}
			var x = document.getElementById("status").innerHTML;			
			document.getElementById('status').innerHTML=x+"<br/><b>Statement Email sent </b>";
		}
    };
	$scope.lsendemail=function()
    {		
		//alert(JSON.stringify($scope.selection));
		if($scope.selectionid.length==0)		
			alert("you have not made any selections");
		else 
		{			
			var d = new Date();						
			var f3 = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();											
			for (i=0;i<$scope.selectionid.length;i++)
			{			
				var sql="report_ledger_sendemail.php?custid="+$scope.selectionid[i]+"&custname="+$scope.selectionname[i]+"&date="+f3+"&flag=1";
				$http.post(sql).success(function(data) {});
			}
			var x = document.getElementById("status").innerHTML;			
			document.getElementById('status').innerHTML=x+"<br/><b>Ledger Email sent</b>";
		}
    };
	
    $scope.clicked=function(a,b,c)
    {				
	var x=document.getElementById(a).checked;		
	if(x)
	{
   	    $scope.selectionid.push(a);
	    $scope.selectionname.push(b);
            $scope.selectionamt.push(c);
	}
    }
	
	$scope.viewledger = function(user)
	{
	   var d = new Date();				
	   var f3 = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();
	   var $sql='report_ledger_sendemail.php?custid=' + user.custid +'&custname='+user.custdesc+"&date="+f3;		
		//alert($sql);		 
		 $window.open($sql);		
	};
	
	
	
	
	$scope.viewstmnt = function(fromdate,user)
	{	
			var d = new Date();				
			var f3 = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear();			
			var f1=document.getElementById("datepicker1").value;						
			if(!f1)			
				var f4=f3;			
			else			
			{
				var f2 = new Date(f1);
				var f4 = ((f2.getDate())+"-"+(f2.getMonth()+1)+"-"+f2.getFullYear());
			}	
		var $sql='report_ledger_bydate_sendemail.php?custid=' + user.custid +'&custname='+user.custdesc+"&date="+f3+"&startdate="+f4;
		// $window.alert($sql);
		 $window.open($sql);
	};
 
 }

   
 